#include "InputSweepDesigner.h"
#include "ui_InputSweepDesigner.h"

#include <string>
#include <sstream>

#include <QMessageBox>

using namespace std;

InputSweepDesigner::InputSweepDesigner(QWidget *parent) :
    InputDesigner(parent),
    ui(new Ui::InputSweepDesigner)
{
    ui->setupUi(this);
}

InputSweepDesigner::~InputSweepDesigner()
{
    delete ui;
}

void InputSweepDesigner::attachInput(RafMat::SimInterface::IConfigurableSPtr ctrl)
{
    m_inputConf = ctrl;

    try
    {
        string amplitude = m_inputConf->getParameter("gain");
        string period = m_inputConf->getParameter("period");
        string offset = m_inputConf->getParameter("offset");
        string phase = m_inputConf->getParameter("phase");
        string freqSta = m_inputConf->getParameter("freqSta");
        string freqEnd = m_inputConf->getParameter("freqEnd");

        ui->cfgAmplitude->setValue(QString::fromStdString(amplitude).toDouble());
        ui->cfgPeriod->setValue(QString::fromStdString(period).toInt());
        ui->cfgOffset->setValue(QString::fromStdString(offset).toDouble());
        ui->cfgPhase->setValue(QString::fromStdString(phase).toDouble());
        ui->cfgStartFreq->setValue(QString::fromStdString(freqSta).toDouble());
        ui->cfgStartFreq->setValue(QString::fromStdString(freqEnd).toDouble());
    }
    catch(std::logic_error &e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}

void InputSweepDesigner::applySettings()
{
    try
    {
        string amplitude = ui->cfgAmplitude->text().toStdString();
        string period = ui->cfgPeriod->text().toStdString();
        string offset = ui->cfgOffset->text().toStdString();
        string phase = ui->cfgPhase->text().toStdString();
        string freqSta = ui->cfgStartFreq->text().toStdString();
        string freqEnd = ui->cfgEndFreq->text().toStdString();


        m_inputConf->setParameter("gain", amplitude);
        m_inputConf->setParameter("period", period);
        m_inputConf->setParameter("offset", offset);
        m_inputConf->setParameter("phase", phase);
        m_inputConf->setParameter("freqSta", freqSta);
        m_inputConf->setParameter("freqEnd", freqEnd);
    }
    catch(std::logic_error &e)
    {
        QMessageBox::warning(this, "Error", e.what());
    }
}

